import 'package:flutter/material.dart';
import '../../model/filespace.dart';

import '../../model/filespace.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import '../../helper/Helper.dart';

import '../../helper/uidata.dart';

class FileSpaceAddPage extends StatefulWidget {
  @override
  FileSpaceAddPage(this.f1, this.fTypeId, this.fCategory, this.col1, this.col2,
      this.col3, this.added);
  final FileSpace f1;
  final int fTypeId;
  final String fCategory;
  final VoidCallback added;
  final String col1;
  final String col2;
  final String col3;

  @override
  FileSpaceAddPageState createState() => new FileSpaceAddPageState();
}

class FileSpaceAddPageState extends State<FileSpaceAddPage> {
  late FileSpace X1;

  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();
  final TextEditingController c3 = new TextEditingController();
  final TextEditingController c4 = new TextEditingController();
  final TextEditingController c5 = new TextEditingController();
  final TextEditingController c6 = new TextEditingController();
  final TextEditingController c7 = new TextEditingController();
  final TextEditingController c8 = new TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.f1 == null) {
      X1 = FileSpace(
          0,
          Helper.GetGuid(),
          widget.fTypeId,
          widget.fCategory,
          "未命名",
          "备注",
          " ",
          " ",
          " ",
          Helper.DateTimeNowString(),
          0,
          " ",
          " ",
          UIData.CurrentUser!.Guid);
    } else {
      X1 = widget.f1;
    }

    c1.text = X1.Name;
    c2.text = X1.Description;
    c3.text = X1.Field1;
    c4.text = X1.Field2;
    c5.text = X1.Field3;
  }

  upload(BuildContext context) async {
    X1.Name = c1.text;
    X1.Description = c2.text;
    X1.Field1 = c3.text;
    X1.Field2 = c4.text;
    X1.Field3 = c5.text;
    print(X1.toJson());
    if (X1.ID == 0) {
      await FileSpace.Add(X1);
    } else {
      await FileSpace.Update(X1);
    }
    // widget.added();
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      appBar: AppBar(
        title: Text("添加空间"),
      ),
      body: FileSpaceAddBody(context),
    );
  }

  FileSpaceAddBody(BuildContext context) => SingleChildScrollView(
        child: FileSpaceAddFields(context),
      );

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];

    list1.add(InputWidget.inCard(
      ListTile(
        leading: IconWidget.CheckImage(),
        title: Text(X1.Category),
      ),
    ));

    list2.add(InputWidget.getInput("空间名称", c1));
    list2.add(Divider(
      height: 0.0,
    ));
    list2.add(InputWidget.getInput2("空间描述", c2));
    list2.add(Divider(
      height: 0.0,
    ));

    list1.add(InputWidget.inCard2(list2));

    list1.add(
        InputWidget.inCard(InputWidget.getButton("保存", () => upload(context))));
    //list1.add(getCLWidget(s1.State==6, Helper.DateTimeFormat(s1.HZQMTM), s1.HZQM));
    return list1;
  }

  FileSpaceAddFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
